﻿//日期格式化与 function.expand中某个方法冲突，单提出使用
//===================日期格式化===========================
//用法
//var date = new Date();
//date.format('yyyy-MM-dd');
//date.format('yyyy-MM-dd HH:mm:ss');
Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1, //month
        "d+": this.getDate(),    //day
        "h+": this.getHours(),   //hour
        "m+": this.getMinutes(), //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter
        "S": this.getMilliseconds() //millisecond
    }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
    (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o) if (new RegExp("(" + k + ")").test(format))
        format = format.replace(RegExp.$1,
        RegExp.$1.length == 1 ? o[k] :
        ("00" + o[k]).substr(("" + o[k]).length));
    return format;
}

//===================日期格式化===========================

//===================计算日期===========================
//用法
//当前时间：2015-1-3
// change_date(-1) == 2015-1-2
var change_date = function (days) {
    // 参数表示在当前日期下要增加的天数
    var now = new Date();
    // + 1 代表日期加，- 1代表日期减
    now.setDate((now.getDate() + 1) + days);
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var day = now.getDate();
    if (month < 10) {
        month = '0' + month;
    }
    if (day < 10) {
        day = '0' + day;
    }

    return year + '-' + month + '-' + day;
};

//===================计算日期===========================

//===================日期格式化===========================
//用法
//str = '/Date(1333245600000+0800)/';
//data_string(str)
function data_string(str) {
    var d = eval('new ' + str.substr(1, str.length - 2));
    var ar_date = [d.getFullYear(), d.getMonth() + 1, d.getDate()];
    for (var i = 0; i < ar_date.length; i++) ar_date[i] = dFormat(ar_date[i]);
    return ar_date.join('-');

    function dFormat(i) { return i < 10 ? "0" + i.toString() : i; }
}



//===================日期格式化===========================